unit USQLEditorControl;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Comctrls, SynMemo, SynHighlighterSQL;

type
  TEditor = class(TSynMemo)
  end;
  TTabSheetEx = class(TTabSheet)
  public
    Editor: TEditor;
  end;
  TSQLEditorControl = class(TPageControl)
  private
    ts1: TTabSheetEx;
    FOnDblClick: TNotifyEvent;
    procedure WMLButtonDblClk(var Message: TWMLButtonDblClk); message WM_LBUTTONDBLCLK;
    function GetActiveEditor: TEditor;
  public
    edtSQLViewer: TEditor;
    SynSQLSyn: TSynSQLSyn;
  public
    constructor Create(AOwner: TComponent); override;
    procedure ShowEditor;
    property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
    property ActiveEditor: TEditor read GetActiveEditor;
  end;

implementation

constructor TSQLEditorControl.Create(AOwner: TComponent);
begin
  inherited;
  ts1 := TTabSheetEx.Create(Self);
  ts1.Parent := Self;
end;

function TSQLEditorControl.GetActiveEditor: TEditor;
begin
  if PageCount < 1 then
    Exit;
  Result := (self.Pages[ActivePageIndex] as TTabSheetEx).Editor;
end;

procedure TSQLEditorControl.ShowEditor;
begin
  ts1.PageControl := Self;
  edtSQLViewer := TEditor.Create(Self);
  SynSQLSyn := TSynSQLSyn.Create(Self);
  edtSQLViewer.Parent := ts1;
  edtSQLViewer.Align := alClient;
  edtSQLViewer.Highlighter := SynSQLSyn;
  SynSQLSyn.SQLDialect := sqlMSSQL2K;
  SynSQLSyn.Enabled := True;
  ts1.Editor := edtSQLViewer;
end;

procedure TSQLEditorControl.WMLButtonDblClk(var Message: TWMLButtonDblClk);
begin
  inherited;
  if Assigned(FOnDblClick) then FOnDblClick(Self);
end;

end.

